<template>
  <main-page>
    <div slot="body">
      <el-form :inline="true" :model="search.productName" size="mini">
        <el-form-item>
          <el-button type="primary" @click="dialogFormVisible = true;">新增</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="tableData" style="width: 100%" row-key="id">
        <!-- :tree-props="{children: 'permissionVos', hasChildren: 'hasChildren'}" -->
        <el-table-column prop="parentId" label="父节点">
          <template slot-scope="scope">
            <template v-if="scope.row.parent">{{scope.row.parent.permissionName}}</template>
            <template v-else>父节点</template>
          </template>
        </el-table-column>
        <el-table-column prop="permissionType" label="资源类型"></el-table-column>
        <el-table-column prop="permissionName" label="资源名称"></el-table-column>
        <el-table-column label="资源图标">
          <template slot-scope="scope">
            <i :class="scope.row.icon"></i>
          </template>
        </el-table-column>
        <el-table-column prop="permissionUrl" label="资源URL"></el-table-column>
        <el-table-column prop="isHome" label="是否为首页">
          <template slot-scope="scope">
            <template v-if="scope.row.isHome == 0">否</template>
            <template v-if="scope.row.isHome == 1">是</template>
          </template>
        </el-table-column>
        <el-table-column prop="orderNum" label="排序"></el-table-column>

        <el-table-column label="创建时间" width="180">
          <template slot-scope="scope">{{scope.row.createTime | datetime}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              title="编辑"
              size="small"
              @click="handleClick2EditById(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              title="删除"
              size="small"
              @click="handleClick2DeleteById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination :listNum="listNum" @currentChange="handleCurrentChange"></pagination>
      <el-dialog title="资源管理" :visible.sync="dialogFormVisible" width="40%">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="父节点" prop="parentId">
            <el-select v-model="form.parentId" style="width:100%">
              <el-option :key="0" label="父节点" :value="0"></el-option>
              <el-option
                v-for="item in parentList"
                :key="item.id"
                :label="item.permissionName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="资源类型" prop="permissionType">
            <el-select v-model="form.permissionType" style="width:100%">
              <el-option key="DIR" label="DIR" value="DIR"></el-option>
              <el-option key="MENU" label="MENU" value="MENU"></el-option>
              <el-option key="BUTTON" label="BUTTON" value="BUTTON"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="资源图标" prop="icon">
            <el-input v-model="form.icon" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="资源名称" prop="permissionName">
            <el-input v-model="form.permissionName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="资源URL" prop="permissionUrl">
            <el-input v-model="form.permissionUrl" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="首页?" prop="isHome">
            <template>
              <el-radio v-model="form.isHome" :label="1">是</el-radio>
              <el-radio v-model="form.isHome" :label="0">否</el-radio>
            </template>
          </el-form-item>
          <el-form-item label="排序" prop="orderNum">
            <el-input v-model="form.orderNum" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="onSubmit">提交</el-button>
        </div>
      </el-dialog>
    </div>
  </main-page>
</template>

<script>
import MainPage from "common/Main";

import {
  getPermissionList,
  savePermission,
  deletePermissionById,
  updatePermissionById,
  getPermissionAllList,
  getPermissionByParentList
} from "api/PermissionApi";

export default {
  name: "permissionList",
  components: {
    MainPage
  },
  data() {
    return {
      tableData: [],
      listNum: 1,
      search: {
        currentIndex: 1
      },
      form: {
        id: "",
        parentId: 0,
        permissionType: "",
        permissionName: "",
        permissionUrl: "",
        isHome: "",
        orderNum: "",
        createTime: "",
        icon: ""
      },
      rules: {
        parentId: [
          { required: true, message: "父节点不能为空", trigger: "blur" }
        ],
        permissionType: [
          { required: true, message: "资源类型不能为空", trigger: "blur" }
        ],
        permissionName: [
          { required: true, message: "资源名称不能为空", trigger: "blur" }
        ],
        permissionUrl: [
          { required: true, message: "资源URL不能为空", trigger: "blur" }
        ],
        isHome: [{ required: true, message: "请选择", trigger: "change" }],
        orderNum: [{ required: true, message: "排序不能为空", trigger: "blur" }]
      },
      dialogFormVisible: false,
      parentList: []
    };
  },
  methods: {
    getPermissionList() {
      getPermissionList(this.search).then(res => {
        if (res) {
          this.tableData = res.datas;
          this.listNum = res.pagination.totalCount;
        }
      });
    },
    handleCurrentChange(page) {
      this.search.currentIndex = page;
      this.getPermissionList();
    },
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.id) {
            //修改
            updatePermissionById(this.form).then(res => {
              if (res) {
                this.$base.showOk();
                this.dialogFormVisible = false;
                this.getPermissionList();
              }
            });
          } else {
            //保存
            savePermission(this.form).then(res => {
              if (res) {
                this.$base.showOk();
                this.dialogFormVisible = false;
                this.getPermissionList();
              }
            });
          }
        }
      });
    },
    handleClick2EditById(row) {
      this.dialogFormVisible = true;
      this.$base.set2Form(this.form, row);
    },
    handleClick2DeleteById(id) {
      this.$base.deleteConfirm(() => {
        deletePermissionById({ id }).then(res => {
          if (res) {
            this.$base.deleteOk();
            this.getPermissionList();
          }
        });
      });
    },
    getPermissionAllList() {
      getPermissionAllList().then(res => {
        if (res) {
          this.parentList = res;
        }
      });
    }
  },
  created() {
    this.getPermissionList();
    this.getPermissionAllList();
  },
  watch: {
    dialogFormVisible(newDialogFormVisible) {
      if (!newDialogFormVisible) {
        this.$base.clearObj(this.form);
        this.$refs.form.resetFields();
      } else this.getPermissionAllList();
    }
  }
};
</script>

<style lang="stylus" scoped></style>
